unit mu_utemp;

interface

uses cc_dict, grids, sysutils, u_units, extctrls, struct, classes, graphics, dialogs;

type

 barray = array[1..1000] of boolean;

 CXYList = class (TObject)
  public
   id,x,y:array[1..1000] of integer;
   count: integer;
   procedure clear;
   procedure add(x,y,id:integer);
   function getx(num:integer): integer;
   function gety(num:integer): integer;
   function getid(num:integer): integer;
   constructor create;
 end;

 CUItem = class (TObject)
  public
   data: RUnit;
   first,second,third: TBitmap;
   procedure assign(u: RUnit);
   destructor destroy; override;
 end;

 CUItemList = class (TObject)
  private
   data: TList;
   index: array[1..1000] of integer;
   lastcount: integer;
  public
   checked: barray;
   constructor Create;
   destructor Destroy; override;
   procedure clear;
   function getItem(id: integer): CUItem;
   function getItemByNum(num: integer): CUItem;
   procedure add(item: CUItem);
   function getCount: integer;
   property count: integer read getCount;
 end;


 CTemplatesList = class (CCDictList)
  private
  public
   fdirname: String;
   procedure loadFromDir(dirname: String);
   procedure render(sg: TStringGrid);
   procedure delete(id: Longint);
   function getTemplateByUnit(u: Runit): string;
 end;

function TlCompareId(Item1, Item2: Pointer): Integer;

var
 gl_tlist: CTemplatesList;
 gl_changed: boolean;
 gl_selid: longint;
 gl_selitems: CUItemList;
 gl_xy: CXYList;
 gl_gogroup: boolean;
 gl_wasmx, gl_wasmy: integer;
 gl_flist: CTemplatesList;
 gl_wasmap: TBitmap;
 gl_lpmx, gl_lpmy: integer;
 gl_flagpoint, gl_cantgo: boolean;
 gl_comindex: array[0..20] of integer;

implementation

function TlCompareId;
begin
 result :=1;
 if (item1 = nil) or (item2 = nil) then exit;
 if CCDict(item1).getValue('id') > CCDict(item2).getValue('id') then result:=-1;
end;

constructor CXYList.create;
begin
 clear;
end;

function CXYList.getx;
begin
 result := x[num];
end;

function CXYList.getid;
begin
 result := id[num];
end;

function CXYList.gety;
begin
 result := y[num];
end;

procedure CXYList.clear;
begin
 fillchar(x,sizeof(x),0);
 fillchar(y,sizeof(y),0);
 fillchar(id,sizeof(id),0);
 count := 0;
end;

procedure CXYList.add;
begin
 count := count+1;
 self.x[count] := x;
 self.y[count] := y;
 self.id[count] := id;
end;

function CUItemList.getCount;
begin
 result := data.count;
end;

function CUItemList.getItemByNum;
var
 i:integer;
begin
 result := nil;
 if index[num] <> -1 then result := CUItem(data.items[index[num]]);
end;

function CUItemList.getItem;
var
 i:integer;
begin
 result := nil;
 for i:=0 to data.count-1 do if CUItem(data.items[i]).data.id = id then begin
  result := CUItem(data.items[i]);
  break;
 end;
end;

procedure CUItemList.clear;
var
 i:integer;
begin
 for i:=0 to data.count-1 do CUItem(data.items[i]).free;
 data.Clear;
 data.pack;
 lastcount := 0;
 fillchar(index,sizeof(index),-1);
 fillchar(checked,sizeof(checked),false);
end;

procedure CUItemList.add;
begin
 lastcount := lastcount+1;
 index[lastcount] := data.Add(item);
end;

constructor CUItemList.create;
begin
 data := TList.create;
 clear;
end;

destructor CUItemList.destroy;
begin
 data.free;
end;

destructor CUItem.destroy;
begin
 first.free;
 second.free;
 third.free
end;

procedure CUItem.assign;
begin
 first.free;
 second.free;
 third.free;
 data := u;
end;

function CTemplatesList.getTemplateByUnit;
var
 i:integer;
begin
 result := '';
 for i:=0 to data.count-1 do begin
 if isTemplateLikeUnit(data.items[i], u) then begin
   result := CCDict(data.items[i]).getValue('e_tname');
  end;
 end;
end;

procedure CTemplatesList.delete;
begin
 inherited delete(id);
 deletefile(fdirname+inttostr(id)+'.dct');
end;

procedure CTemplatesList.loadFromDir;
begin
 fdirname := dirname;
 inherited loadFromDir(dirname);
end;

procedure CTemplatesList.render;
var
 i, j: integer;
 item: CCDict;
begin
 if data.count = 0 then sg.rowcount := 2 else sg.RowCount := data.Count+1;
 for i:=0 to 4 do sg.cells[i,1]:='';
 refreshindexes;
 for i:=0 to data.count-1 do begin
  item := CCDict(data.items[i]);
  sg.cells[1,data.count-i] := item.getValue('e_tname');
  sg.cells[0,data.count-i] := item.getValue('id');
  sg.cells[2,data.count-i] := orders[strtoint(item.getValue('rb_comand'))];
  sg.cells[3,data.count-i] := item.getValue('cb_target');
  sg.cells[4,data.count-i] := item.getValue('cb_enemy');
 end;
end;

end.
